'''
Created on 08.03.2013

@author: virthead
'''
import webapp2
import datetime

from webapp2_extras.i18n import gettext as _

from google.appengine.api import users
from apnetmodels.basicmodels import immeuble, period_concept, fundraising 
from common import functions
from BaseHandler import BaseHandler

from xhtml2pdf import pisa
import cStringIO

class ReportsWeb(BaseHandler):
    def __init__(self, *args, **kwargs):
        super(ReportsWeb, self).__init__(*args, **kwargs)
        self.au = functions.GetAPNETUser()
        if not self.au:
            self.redirect(users.create_login_url(self.request.uri))
            return
        self.format = self.request.GET.get('format', 'html')
        if self.format != 'pdf':
            self.format = 'html'
        self.fields = {
                       'immeuble': _('Tenement')
                       }
    
    def makePDF(self, report_html):
        report_pdf = cStringIO.StringIO()
        pdf = pisa.CreatePDF(report_html, report_pdf)
        if not pdf.err:
            self.response.headers['Content-Type'] = 'application/pdf'
            self.response.out.write(report_pdf.getvalue())
        else:
            self.response.out.write(report_html)
        report_pdf.close()
    
    def get(self, *args, **kwargs):
        self.get_method(self.__class__, *args, **kwargs)
    
    def op_fundraising(self, *args, **kwargs):
        imm_id = kwargs.get('imm_id', -1)
        frreq_id = kwargs.get('ap_id', -1)
        
        imm = immeuble.get_by_id(int(imm_id))
        if not imm:
            self.redirect(webapp2.uri_for('mainpage'))
            return
        
        fr_req = fundraising.get_by_id(int(frreq_id))
        if not fr_req:
            self.redirect(webapp2.uri_for('mainpage'))
            return
        
        description = fr_req.desc
        pages, calc_sum, budget_sum, ck_merged = fr_req.period.get_fundraising_distribution_lo_print(fr_req.amount, fr_req.type)
        
        template_values = {
                           'pages': pages,
                           'deadline': fr_req.deadline,
                           'description': description,
                           'app_user': self.au,
                           'immeuble': imm,
                           'ck_merged': ck_merged,
                           'fr_sum': fr_req.amount,
                           'calc_sum': calc_sum,
                           'budget_sum': budget_sum,
                           'period': fr_req.period,
                           'fields': self.fields
        }

        template = self.jinja2.get_template('reports/fundraising.html')
        report_html =  template.render(template_values)
        if self.format == 'html':
            self.response.out.write(report_html)
        else:
            self.makePDF(report_html)
 
    
    def op_lots(self, *args, **kwargs):
        imm_id = kwargs.get('imm_id', -1)
        
        imm = immeuble.get_by_id(int(imm_id))
        if not imm:
            self.redirect(webapp2.uri_for('mainpage'))
            return
        
        syn = imm.syndic
        
        data0 = imm.get_lots()
        
        todaydate = datetime.datetime.utcnow()
        
        template_values = {
                           'syndic': syn,
                           'data0': data0,
                           'todaydate': todaydate,
                           'app_user': self.au,
                           'immeuble': imm,
                           'fields': self.fields
        }

        template = self.jinja2.get_template('reports/lots_list.html')
        report_html = template.render(template_values)
        if self.format == 'html':
            self.response.out.write(report_html)
        else:
            self.makePDF(report_html)
    
    def op_budget(self, *args, **kwargs):
        
        imm_id = kwargs.get('imm_id', -1)
        ap_id = kwargs.get('ap_id', -1)
        
        imm = immeuble.get_by_id(int(imm_id))
        if not imm:
            self.redirect(webapp2.uri_for('mainpage'))
            return
        
        cp = period_concept.get_by_id(int(ap_id))
        if not cp:
            self.redirect(webapp2.uri_for('mainpage'))
            return
        
        budget_reg, pc_prev, pc_next = cp.get_budget_template_v2(b_type='Regular')
        budget_add, pc_prev, pc_next = cp.get_budget_template_v2(b_type='Additional')
        
        todaydate = datetime.datetime.utcnow()
        
        template_values = {
                           'immeuble': imm,
                           'budget_reg': budget_reg,
                           'budget_add': budget_add,
                           'pc_current': cp,
                           'pc_prev': pc_prev,
                           'pc_next': pc_next,
                           'todaydate': todaydate,
                           'app_user': self.au,
                           'fields': self.fields
                           }

        template = self.jinja2.get_template('reports/budget.html')
        report_html = template.render(template_values)
        if self.format == 'html':
            self.response.out.write(report_html)
        else:
            self.makePDF(report_html)
